//
//  ThemeManager.swift
//  ScarnKing
//
//  Created by ninebot on 2025/7/22.
//

import UIKit

class ThemeManager {
    static let shared = ThemeManager()
    
    private let userDefaults = UserDefaults.standard
    private let darkModeKey = "DarkModeEnabled"
    
    var isDarkMode: Bool {
        get {
            return userDefaults.bool(forKey: darkModeKey)
        }
        set {
            userDefaults.set(newValue, forKey: darkModeKey)
            applyTheme()
        }
    }
    
    private init() {}
    
    func applyTheme() {
        // 获取当前窗口
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first else { return }
        
        // 应用主题
        window.overrideUserInterfaceStyle = isDarkMode ? .dark : .light
        
        // 发送通知，让其他页面更新UI
        NotificationCenter.default.post(name: .themeDidChange, object: nil)
    }
    
    // MARK: - 颜色定义
    struct Colors {
        static func background(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.05, green: 0.05, blue: 0.07, alpha: 1.0) : UIColor(red: 0.95, green: 0.95, blue: 0.97, alpha: 1.0)
        }
        
        static func cardBackground(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.12, green: 0.12, blue: 0.14, alpha: 1.0) : .white
        }
        
        static func text(for isDark: Bool) -> UIColor {
            return isDark ? .white : .black
        }
        
        static func secondaryText(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0) : .gray
        }
        
        static func border(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0) : UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0)
        }
        
        static func proBannerBackground(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.18, green: 0.18, blue: 0.2, alpha: 1.0) : UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
        }
        
        static func tabBarUnselectedColor(for isDark: Bool) -> UIColor {
            return isDark ? UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0) : .gray
        }
    }
}

// MARK: - Notification Extension
extension Notification.Name {
    static let themeDidChange = Notification.Name("ThemeDidChange")
}

// MARK: - UIViewController Extension
extension UIViewController {
    func updateTheme() {
        let isDark = ThemeManager.shared.isDarkMode
        
        // 更新背景色
        view.backgroundColor = ThemeManager.Colors.background(for: isDark)
        
        // 更新导航栏
        navigationController?.navigationBar.barStyle = isDark ? .black : .default
        navigationController?.navigationBar.tintColor = isDark ? .white : .black
        navigationController?.navigationBar.barTintColor = ThemeManager.Colors.cardBackground(for: isDark)
        
        // 更新状态栏
        setNeedsStatusBarAppearanceUpdate()
    }
} 
